﻿using Model.Models;
using Prism.Commands;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using PrismToWPF.Common;
using PrismToWPF.Extensions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrismToWPF.ViewModels
{
    public class MainWindowViewModel : BindableBase, IConfigureService
    {
        private readonly IRegionManager regionManager;
        private IRegionNavigationJournal journal;
        private readonly IDialogHostService dialog;
        private readonly IContainerProvider containerProvider;
        public MainWindowViewModel(IContainerProvider containerProvider,
            IRegionManager regionManager, IDialogHostService dialog)
        {
            NavigateCommand = new DelegateCommand<MenuModel>(Navigate);
            MenuList = new ObservableCollection<MenuModel>();
            this.containerProvider = containerProvider;
            this.regionManager = regionManager;
        }
        private ObservableCollection<MenuModel> menuList;
        public ObservableCollection<MenuModel> MenuList
        {
            get { return menuList; }
            set { menuList = value; RaisePropertyChanged(); }
        }
        public DelegateCommand<MenuModel> NavigateCommand { get; private set; }
        /// <summary>
        /// 获取菜单列表
        /// </summary>
        private void GetMenus()
        {
            MenuList.Add(new MenuModel() { Id = 1, Title = "用户管理", MenuRoute = "UserView",Icon="" });
            MenuList.Add(new MenuModel() { Id = 1, Title = "角色管理", MenuRoute = "UserView", Icon = "" });
            MenuList.Add(new MenuModel() { Id = 1, Title = "权限管理", MenuRoute = "UserView", Icon = "" });
        }
        private void Navigate(MenuModel obj)
        {
            if (obj == null || string.IsNullOrWhiteSpace(obj.MenuRoute))
                return;

            regionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate(obj.MenuRoute, back =>
            {
                journal = back.Context.NavigationService.Journal;
            });
        }
        /// <summary>
        /// 首页初始化
        /// </summary>
        public async void Configure()
        {
            GetMenus();
            regionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate("UserView");
            DialogParameters param = new DialogParameters();
            var dialogResult = await dialog.ShowDialog("UserView", param);
            if (dialogResult.Result == ButtonResult.OK)
            { }
        }
    }
}
